﻿using ETW.EventListeners;
using ETW.EventListeners.Listeners;
using ETW.EventListeners.ObjectValues;
using System;
using System.Diagnostics.Tracing;

namespace WebApplication1
{
    public static class EventListennerHelper
    {
        static HostingEventListener HostingEventListener = null;
        static HttpEventListenner HttpEventListenner = null;
        static RuntimeEventListener RuntimeEventListener = null;
        public static void Start()
        {
            DefaultConfiguration.HostingEventListenerOptions = new ETW.EventListeners.Options.EventListenerOptions { EventCounterIntervalSec = 5 };
            DefaultConfiguration.HttpEventListenerOptions = new ETW.EventListeners.Options.EventListenerOptions { EventCounterIntervalSec = 5 };
            DefaultConfiguration.RuntimeEventListenerOptions = new ETW.EventListeners.Options.EventListenerOptions { EventCounterIntervalSec = 5 };

            //HostingEventListener = new HostingEventListener();
            HttpEventListenner = new HttpEventListenner();
            //RuntimeEventListener = new RuntimeEventListener();
            //BindHostingEventListenerEvent();
            BindHttpEventListennerEvent();
            //BindRuntimeEventListenerEvent();
        }

        public static void Stop()
        {
            if (HostingEventListener != null)
            {
                HostingEventListener.Dispose();
                HostingEventListener = null;
            }
            if (HttpEventListenner != null)
            {
                HttpEventListenner.Dispose();
                HttpEventListenner = null;
            }
            if (RuntimeEventListener != null)
            {
                RuntimeEventListener.Dispose();
                RuntimeEventListener = null;
            }
        }

        private static void BindHostingEventListenerEvent()
        {
            HostingEventListener.OnCurrentRequestsWritten += LogHostingMeanCounterPayload;
            HostingEventListener.OnFailedRequestsWritten += LogHostingMeanCounterPayload;
            HostingEventListener.OnRequestRateWritten += LogHostingSumCounterPayload;
            HostingEventListener.OnTotalRequestsWritten += LogHostingMeanCounterPayload;
        }
        private static void BindHttpEventListennerEvent()
        {
            HttpEventListenner.OnCurrentRequestsWritten += LogHttpMeanCounterPayload;
            HttpEventListenner.OnHttp11ConnectionsCurrentTotalWritten += LogHttpSumCounterPayload;
            HttpEventListenner.OnHttp11RequestsqueueDurationWritten += LogHttpMeanCounterPayload;
            HttpEventListenner.OnHttp20ConnectionsCurrentTotalWritten += LogHttpSumCounterPayload;
            HttpEventListenner.OnHttp20RequestsqueueDurationWritten += LogHttpMeanCounterPayload;
            HttpEventListenner.OnRequestsFailedRateWritten += LogHttpMeanCounterPayload;
            HttpEventListenner.OnRequestsFailedWritten += LogHttpSumCounterPayload;
            HttpEventListenner.OnRequestsStartedRateWritten += LogHttpMeanCounterPayload;
            HttpEventListenner.OnRequestsStartedWritten += LogHttpSumCounterPayload;
        }
        private static void BindRuntimeEventListenerEvent()
        {
            RuntimeEventListener.OnActiveTimerCountWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnAllocRateWritten += LogHttpSumCounterPayload;
            RuntimeEventListener.OnAssemblyCountWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnCpuUsageWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnExceptionCountWritten += LogHttpSumCounterPayload;
            RuntimeEventListener.OnGcCommittedBytesWritten += LogHttpSumCounterPayload;
            RuntimeEventListener.OnGcFragmentationWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnGCHeapSizeWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnGen0GcCountWritten += LogHttpSumCounterPayload;
            RuntimeEventListener.OnGen0GcSizeWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnGen1GcCountWritten += LogHttpSumCounterPayload;
            RuntimeEventListener.OnGen1GcSizeWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnGen2GcCountWritten += LogHttpSumCounterPayload;
            RuntimeEventListener.OnGen2GcSizeWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnILBytesJittedWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnLOHSizeWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnMethodJittedCountWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnMonitorLockContentionCountWritten += LogHttpSumCounterPayload;
            RuntimeEventListener.OnPOHSizeWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnThreadPoolCompletedTimesCountWritten += LogHttpSumCounterPayload;
            RuntimeEventListener.OnThreadPoolQueueLengthWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnThreadPoolThreadCountWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnTimeInGcWritten += LogHttpMeanCounterPayload;
            RuntimeEventListener.OnWorkingSetWritten += LogHttpMeanCounterPayload;
        }

        private static void LogHttpSumCounterPayload(SumCounterPayload sumCounterPayload, EventWrittenEventArgs eventData)
        {
            Console.WriteLine($"Http  {DateTime.Now.ToLongTimeString()}=>名称【{sumCounterPayload.Name}】显示名称【{sumCounterPayload.DisplayName}】 显示速率【{sumCounterPayload.DisplayRateTimeScale}】" +
                $"增量【{sumCounterPayload.Increment}】 间隔时间(秒)【{sumCounterPayload.IntervalSec}】 计数器类型【{sumCounterPayload.CounterType}】 " +
                $"元数据【{sumCounterPayload.Metadata}】 显示单位【{sumCounterPayload.DisplayUnits}】");
        }

        private static void LogHttpMeanCounterPayload(MeanCounterPayload meanCounterPayload, EventWrittenEventArgs eventData)
        {
            Console.WriteLine($"Http   { DateTime.Now.ToLongTimeString()}=>名称【{ meanCounterPayload.Name}】 显示名称【{meanCounterPayload.DisplayName}】 平均值【{meanCounterPayload.Mean}】 " +
                $"标准差【{meanCounterPayload.StandardDeviation}】 计数【{meanCounterPayload.Count}】最小值【{meanCounterPayload.Min}】 最大值【{meanCounterPayload.Max}】 " +
                $"间隔时间(秒)【{meanCounterPayload.IntervalSec}】 系列【{meanCounterPayload.Series}】 计数器类型【{meanCounterPayload.CounterType}】 " +
                $"元数据【{meanCounterPayload.Metadata}】 显示单位【{meanCounterPayload.DisplayUnits}】");
        }

        private static void LogHostingSumCounterPayload(SumCounterPayload sumCounterPayload, EventWrittenEventArgs eventData)
        {
            Console.WriteLine($"Hosting  {DateTime.Now.ToLongTimeString()}=>名称【{sumCounterPayload.Name}】显示名称【{sumCounterPayload.DisplayName}】 显示速率【{sumCounterPayload.DisplayRateTimeScale}】" +
                $"增量【{sumCounterPayload.Increment}】 间隔时间(秒)【{sumCounterPayload.IntervalSec}】 计数器类型【{sumCounterPayload.CounterType}】 " +
                $"元数据【{sumCounterPayload.Metadata}】 显示单位【{sumCounterPayload.DisplayUnits}】");
        }

        private static void LogHostingMeanCounterPayload(MeanCounterPayload meanCounterPayload, EventWrittenEventArgs eventData)
        {
            Console.WriteLine($"Hosting  { DateTime.Now.ToLongTimeString()}=>名称【{ meanCounterPayload.Name}】 显示名称【{meanCounterPayload.DisplayName}】 平均值【{meanCounterPayload.Mean}】 " +
                $"标准差【{meanCounterPayload.StandardDeviation}】 计数【{meanCounterPayload.Count}】最小值【{meanCounterPayload.Min}】 最大值【{meanCounterPayload.Max}】 " +
                $"间隔时间(秒)【{meanCounterPayload.IntervalSec}】 系列【{meanCounterPayload.Series}】 计数器类型【{meanCounterPayload.CounterType}】 " +
                $"元数据【{meanCounterPayload.Metadata}】 显示单位【{meanCounterPayload.DisplayUnits}】");
        }

    }
}
